<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * =================================================================
 * Tokaban Standard System.
 * CodeIniter Tokaban framework for PHP.
 *
 * @package		: CI-TKB 
 * @author		: Tokaban R&D Team.
 * 				: hung_pn89
 * @copyright	: Copyright (c) 2009, Tokaban, Inc.
 * @since		: Version 2.0
 * =================================================================
 */    
   
/**
 *------------------------------------------------------------------
 * do_advertising class
 *
 * Quan ly them, sua thong tin danh muc quan huyen
 *
 * @subpackage	controllers
 * @category	
 * @author		Pham Quang Tri 
 *------------------------------------------------------------------
 */ 
class do_advertising extends Controller 
{	
	var $m_language 		= ''; // nhan ngon ngu tu file language
	var $m_nid_user_login 	= ''; // nhan iduser tu session

	var $m_nid 				= ''; // nhan nid cua phuong xa can chinh sua
	var $m_event			= ''; // nhan su kien cap nhat hoac bo qua
	var $m_button_click		= ''; // nhan su kien tu hidden button o trang tkb_advertising
	
	var	$m_link_page  		= ''; // nhan link cua su kien
	var $m_link_cancel 		= '';  // link toi trang tkb_advertising_listview

	var $m_form_title 		= ''; // tieu de cua form
	
	// Nhung bien dung cho doi tuong.			
	var $m_txt_nid 				= ''; // nhan ma tin tuc
	var $m_txt_ccode			= '';
	var $m_txt_cname			= '';	
	var $m_txt_cimage			= ''; // nhan duong dan hinh anh
	var $m_txt_clink			= '';
	var $m_txt_dstartdate		= '';
	var $m_txt_denddate			= '';
	var $m_txt_nid_user1		= '';
	var $m_hidden_image_old		= '';
	var $m_txt_cstatus			= '';
	
	var $m_error_ctext		 	= '';
	var $m_error_clink		 	= '';	
	var $m_error_cimage			= '';
	
	// Cac bien can xuat hien thi thong bao len view cho nguoi dung xem.
		
/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com
 * @finished date	: 2009/12/13
 * @description		: Ham khoi tao, load cac thu vien can dung cho class
 * @access	        : public
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */	
function do_advertising()
	{ 			
		
		parent::Controller();
		session_start();
		//Load cac thu vien he thong.
		$this->load->database();
		
		$this->load->helper('ap_db');	
		$this->load->helper('ap_function');
		$this->load->helper('ap_html');
		$this->load->helper('ap_view');
		$this->load->helper('ap_object');
		$this->load->helper('ap_fck'); // load thu vien de xuat ra trinh soan thao van ban
				
				
		// Kiem tra dieu kien login theo ma so he thong 1
		$this->tokaban_system_check = '1';
		// Load cac thu vien rieng can thiet khac neu co
		$this->load->model('advertising_model');
		init_fck();
		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Lay nid tu view tkb_city_listview
 * @access	        : public
 *
 * @param string	: $nid   :  
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_edit($nid) // ham chinh sua, tham so la nid cua phuong xa can chinh sua
	{
		$this->m_event = 'edit';
		$this->m_nid   = $nid;		 	
		$this->do_process();		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_update_edit() // ham update cac thong tin da chinh sua
	{	
		$this->m_event = 'update_edit';		
		$this->do_process();
	}
	
/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_add() // ham them phuong xa
	{				
		$this->m_event 	= 'add';
		$this->m_nid 	= '0';
		$this->do_process();
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_update_add() // ham cap nhat thong tin cua phuong xa moi can tao
	{		
		$this->m_event = 'update_add';
		$this->do_process();		
	}
	
/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: ham upload hinh anh
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */	
	
function upload_img($file,$path)
{
	$file_name='';
	if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg")|| ($file["type"] == "image/pjpeg"))&& ($file["size"] < 2000000))
		{
		if ($file["error"] > 0)
			{
				$msg= "Co loi khi upload anh: " . $_FILES["image2"]["error"] . "<br />";
				
 	 		}
			else
			{
			if (file_exists("upload/" . $file["name"]))
			  {
				$msg= $file["name"] . " Files da ton tai. ";
			  }
			else
			  {
			  list($name,$ext) = preg_split('[.]',$file["name"]);
			  //Upload image vao thu muc nao
			  $file_name=md5(rand(2,999).date('Ymdhis')).'.'.$ext;
			  $this->m_txt_cimage =$file_name;
			  move_uploaded_file($file["tmp_name"],$path.$file_name);
			  }
			}
		
		}	
	return 	$file_name;
}


/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/11/11
 * @description		: Goi tuan tu cac ham theo dung quy dinh ve luong du lieu
 * @access	        : public
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function do_process() // ham xu ly
	{
		$this->get_data();
		$this->caculate_data();
		$this->do_business();
		$this->destroy_data();
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Nhan du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */
private function get_data()
	{
		// Lay nid user tu session
		$this->m_nid_user_login = Fget_userdata('session_nid_user');
		// Load file ngon ngu can su dung
		$this->load->language('ap', 'vni');
		
		// Xac dinh cac gia tri duoc post tu view.
		
		// Kiem tra hinh anh duoc upload
		if(isset($_FILES["txt_cimage"]))
		{
			
		 	$path						= './upload/taitro/';
			$this->m_txt_cimage 		= $this->upload_img($_FILES["txt_cimage"],'../'.$path);
			if($this->m_txt_cimage != '')
				$this->m_hidden_image_old	= $this->m_txt_cimage;		
			else
				$this->m_hidden_image_old	= $_POST['hidden_image_old'];									
		}

		
		// Can kiem tra tren tung form cu the.
		if (isset($_POST['txt_cname']))
		{	
			
			$this->m_txt_cname			= $_POST['txt_cname'];
			$this->m_txt_clink			= $_POST['txt_clink'];
		}
		if (isset($_POST['txt_cname']))
			$this->m_txt_ctext			= $_POST['txt_cname'];
		if (isset($_POST['txt_cname']))
			$this->m_txt_cstatus			= $_POST['txt_cname'];	
		// Kiem tra va nhan cac bien hidden neu co.

		if (isset($_POST['hidden_nid']))
			$this->m_nid 			= $_POST['hidden_nid'];
			
		if (isset($_POST['hidden_event']))
			$this->m_event			= $_POST['hidden_event'];
			
		if (isset($_POST['hidden_button_click']))
			$this->m_button_click	= $_POST['hidden_button_click'];
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Tinh toan du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function caculate_data()
	{
		// Link khi click nut cancel
		$this->m_link_cancel = base_url() . 'index.php/do_advertising_listview';		
		// Xu ly tuy theo su kien.

		switch ($this->m_event)
		{
			case 'edit':										
				$this->m_form_title = $this->lang->line('lbl.advertising.FormEditTitle');
				$this->m_link_page 	= base_url() . 'index.php/do_advertising/f_update_edit';	
				
				// Chi trong truong hop edit thi moi lay thong tin doi tuong.
				// Chi to chuc bien va nhan nhung thong tin can thiet phuc vu cho viec xu ly ma thoi.
				$advertising = $this->advertising_model->get_byid($this->m_nid);
				$this->m_hidden_image_old	= $advertising['cimage'];
				$this->m_txt_cname			= $advertising['ctext'];
				$this->m_txt_cimage  		= $advertising['cimage'];
				$this->m_txt_clink			= $advertising['clink'];
				$this->m_txt_dstartdate		= $advertising['ddate01'];
				$this->m_txt_denddate		= $advertising['ddate02'];
				
						 
				$this->m_event 	= 'update_edit';	
				break;
						
			case 'add':
				$this->m_form_title = $this->lang->line('lbl.advertising.FormAddTitle');
				$this->m_link_page 	= base_url() . 'index.php/do_advertising/f_update_add';
				$this->m_event 	= 'update_add';
				break;
				
			case 'update_edit':
				$advertising = $this->advertising_model->get_byid($this->m_nid);
				if ($this->m_button_click == 'btn_submit')
						if ($this->update_data()==TRUE)
							redirect ('do_advertising_listview');

				$this->m_form_title = $this->lang->line('lbl.advertising.FormEditTitle');
				$this->m_link_page 	= base_url() . 'index.php/do_advertising/f_update_edit';
				break;
			
			case 'update_add':

				if ($this->m_button_click == 'btn_submit')
						if ($this->insert_data()==TRUE)
							redirect ('do_advertising_listview');
						
				$this->m_form_title = $this->lang->line('lbl.advertising.FormAddTitle');
				$this->m_link_page 	= base_url() . 'index.php/do_advertising/f_update_add';
				break;
		}		

	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Xu ly nghiep vu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function do_business()
	{
								
		$data['event'] 				= $this->m_event;
		$data['menu'] = Fget_menu_html($this->m_nid_user_login);
								
		$data['lbl_form_title'] 	= $this->m_form_title;
		
		$data['link_page'] 			= $this->m_link_page;
		$data['link_cancel'] 		= $this->m_link_cancel;		
		
		// Ten cac button		
		$data['btn_update'] 		= $this->lang->line('btn.0000.Update');
		$data['btn_cancel'] 		= $this->lang->line('btn.0000.Cancel');
		$data['fr_img']				= Fstr_replace('admin/','',base_url());
		// Ky hieu dung de xac dinh cac truong thong tin khong duoc phep thieu.
		$data['get_icon_notnull']   	= Fget_icon_notnull();		
		$data['get_message_notnull']   	= Fget_icon_notnull() . $this->lang->line('msg.0000.NotNullValue');;
		
		$data['nid'] 				= $this->m_nid;
//			$data['gencbo_city_list']		= Fgen_html_combobox('no', 'cbof_nid_city', $this->m_cbof_nid_city, '', $this->m_obj_city_view, 'nid', 'ccity','nosubmit');
		
		// tieu de form
		$data['lbl_form_title'] 	= $this->m_form_title;
		$data['menu_active']		= 'article';
		// Tieu de cac truong
		$data['lbl_nid']          	= $this->lang->line('lbl.advertising.nid');
		$data['lbl_cname']			= $this->lang->line('lbl.advertising.name');
		$data['lbl_image']			= $this->lang->line('lbl.advertising.image');
		$data['lbl_clink']			= $this->lang->line('lbl.advertising.link');
		$data['lbl_img']			= $this->lang->line('lbl.advertising.img');
		$data['lbl_dstartdate']		= $this->lang->line('lbl.advertising.startdate');
		$data['lbl_denddate']		= $this->lang->line('lbl.advertising.enddate');
		// Gia tri hien thi
		$data['txt_nid'] 			= $this->m_txt_nid;
		$data['txt_cname'] 			= $this->m_txt_cname;
		if($this->m_txt_cimage == '')
		$this->m_txt_cimage			= $this->m_hidden_image_old;
		$data['txt_cimage'] 		= $this->m_txt_cimage;
		$data['txt_clink'] 			= $this->m_txt_clink;
		$data['txt_dstartdate'] 	= $this->m_txt_dstartdate;
		$data['txt_denddate'] 		= $this->m_txt_denddate;
	
		// Meszsage thong bao loi
		$data['error_clink']		= $this->m_error_clink;
		$data['error_ctext']		= $this->m_error_ctext;		
		$data['error_cimage']		= $this->m_error_cimage;
		
		// Load view voi su kien tuong ung.
		$this->load->view('advertising_view/index.php',$data);
		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Huy du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function destroy_data()
    {
        	
    }


/**
 |====================================================================
 | DANH SACH CAC HAM DINH NGHIA THEM
 |====================================================================
 */
private function check_valid_not_null()
	{

		// Kiem tra truong cimage khong rong.

		
		if(trim($this->m_hidden_image_old) == '')
		{
			$this->m_error_cimage	='Hình ảnh '. $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		} 

		if(trim($this->m_txt_cname) == '')
		{
			$this->m_error_ctext	= 'Tên tài trợ '. $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		if(trim($this->m_txt_clink) == '')
		{
			$this->m_error_clink	= 'Link liên kết' . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		} 
		// Kiem tra truong  khong rong
				
		
		return TRUE;

	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Phan Ngoc Hung - hung_pn89@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Kiem tra cac truong du lieu nhung khong co kiem tra truong image
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		

private function check_valid_not_null_image()
	{

		if(trim($this->m_txt_ctext) == '')
		{
			$this->m_error_ctext	= $this->lang->line('lbl.advertising.text') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		if(trim($this->m_txt_clink) == '')
		{
			$this->m_error_clink	= $this->lang->line('lbl.advertising.link') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		} 
		// Kiem tra truong  khong rong
				
		
		return TRUE;

	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Kiem tra du lieu truoc khi insert
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function check_valid_before_insert()
	{
		if ($this->check_valid_not_null()== FALSE)
			return FALSE;
					
		/*if (fbcheck_exists_key_addnew('tadvertising', 'nid',$this->m_txt_nid)==FALSE)
		{
			$this->m_error_nid	 	= $this->lang->line('lbl.advertising.nid') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}*/
		
		/*if (fbcheck_exists_key_addnew('advertising', 'cimage',$this->m_txt_cimage)==FALSE)
		{
			$this->m_error_cimage	 	= $this->lang->line('lbl.advertising.img') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}*/
		
		
	
		return TRUE;		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Insert du lieu vao tcity
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function insert_data()
	{
		if ($this->check_valid_before_insert())
		{
				$data =	array(			
					
				'cimage'		=> $this->m_hidden_image_old,
				'ctext'			=> $this->m_txt_ctext,
				'cstatus'		=> 1,				
				'clink'			=> $this->m_txt_clink,
				'niduser01'     => $this->m_nid_user_login,
				'ddate01'		=> dbget_current_date(),
								);
			// Goi phuong thuc cap nhat thong tin vao database.	
			
			$this->advertising_model->insert($data);		
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Kiem tra du lieu truoc khi update
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function check_valid_before_update()
	{
		if ($this->check_valid_not_null_image()== FALSE)
			return FALSE;
		
				
	/*	if (fbcheck_exists_key_update('tadvertising', 'nid',$this->m_txt_nid, $this->m_nid)==FALSE)
		{
			$this->m_error_nid	 	= $this->lang->line('lbl.advertising.nid') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}*/
		
		/*if (fbcheck_exists_key_update('advertising', 'cimage',$this->m_txt_cimage, $this->m_cimage)==FALSE)
		{
			$this->m_error_cimage	 	= $this->lang->line('lbl.advertising.cimage') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}*/
		
		
		return TRUE;
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Pham Quang Tri - tri_pq88@tokaban.com

 * @finished date	: 2009/12/13
 * @description		: Update du lieu vao tcity
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function update_data()
	{	
		if ($this->check_valid_before_update())
		{
			$data =	array(	
			
				'ctext' 		=> $this->m_txt_cname,
				'clink' 		=> $this->m_txt_clink,
				'niduser02'		=> $this->m_nid_user_login
				
		        );
				if($this->m_hidden_image_old !='')
			{
				$path						='.././upload/images/';
				delfile($path.$this->m_hidden_image_old);
				$data	['cimage']	=  $this->m_hidden_image_old ;
			}

			$this->advertising_model->update_bynid($this->m_nid, $data);		
			return TRUE; 
		}
		else
		{
			return FALSE;
		}
	}


}
// End do_advertising class
	
// End of file do_advertising.php
// Location: controllers/do_advertising.php



